﻿using System;
using System.Globalization;

namespace Cactussoft.Common
{
    /// <summary>
    /// Static class-container for extension methods (extensions).
    /// </summary>
    public static class StringExtensions
    {
        #region String extensions

        /// <summary>
        /// Checks if string empty or not.
        /// </summary>
        /// <param name="arg">String.</param>
        /// <returns>True if string is empty.</returns>
        public static bool IsEmpty(this string arg)
        {
            return String.IsNullOrEmpty(arg);
        }

        /// <summary>
        /// Checks if string empty or not.
        /// </summary>
        /// <param name="arg">String.</param>
        /// <returns>True if string is NOT empty.</returns>
        public static bool IsNotEmpty(this string arg)
        {
            return !String.IsNullOrEmpty(arg);
        }

        /// <summary>
        /// Checks equality of strings with ignoring string casing.
        /// </summary>
        /// <param name="source">Source string.</param>
        /// <param name="destination">Destination string.</param>
        /// <returns>True if strings are equal with ignoring casing.</returns>
        public static bool EqualsIgnoreCase(
            this string source, string destination)
        {
            return !(source.IsEmpty()) && source.Equals(destination, 
                                                        StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// Checks if source string starts with destination with ignoring string casing.
        /// </summary>
        /// <param name="source">Source string.</param>
        /// <param name="destination">Destination string.</param>
        /// <returns>True if source string starts with destination with ignoring string casing.</returns>
        public static bool StartsWithIgnoreCase(
            this string source, string destination)
        {
            return !(source.IsEmpty()) && source.StartsWith(destination,
                                                            StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// Returns string with applied format, arguments and format provider.
        /// 
        /// Exceptions:
        ///     <see cref="ArgumentNullException">ArgumentNullException</see> in case when provider is null.
        ///     <see cref="FormatException">FormatException</see> in case when string.Format(...) raised this exception.
        /// </summary>
        /// <param name="format">format string.</param>
        /// <param name="provider"><see cref="IFormatProvider">provider</see>.</param>
        /// <param name="args">paramenters for format string.</param>
        /// <returns>string as result.</returns>
        public static string FormatWith(
            this string format, IFormatProvider provider, params object[] args)
        {
            if (format == null)
                throw new ArgumentNullException("format");

            return string.Format(provider, format, args);
        }

        /// <summary>
        /// Returns string with applied format, arguments and format provider.
        /// 
        /// Exceptions:
        ///     <see cref="ArgumentNullException">ArgumentNullException</see> or <see cref="FormatException">FormatException</see>.
        /// </summary>
        /// <param name="format">format string.</param>
        /// <param name="args">paramenters for format string.</param>
        /// <returns>string as result.</returns>
        public static string FormatWithInvariant(
            this string format, params object[] args)
        {
            if (format == null)
                throw new ArgumentNullException("format");

            return string.Format(CultureInfo.InvariantCulture, format, args);
        }

        #endregion
    }
}
